package com.hdd.biz.app.controller;

import com.github.houbb.heaven.util.id.impl.UUID32;
import com.hdd.biz.app.dto.*;
import com.hdd.biz.app.entity.WxParamModel;
import com.hdd.biz.app.service.HomeService;
import com.hdd.biz.finance.dto.PaymetListRequest;
import com.hdd.biz.member.entity.Member;
import com.hdd.biz.member.service.MemberService;
import com.hdd.biz.painting.dto.WorksRequest;
import com.hdd.config.Constant;
import com.hdd.dto.SuperResponse;
import com.hdd.utils.IdUtils;
import com.hdd.utils.ImageUtil;
import com.hdd.utils.JsonUtil;
import com.hdd.utils.WxQrCode;
import com.hdd.utils.wx.RedisUtil;
import com.hdd.utils.wx.WxMiniProgramUtil;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/app/home")
@Api(value = "小程序-首页",tags = {"小程序-首页"})
public class HomeController {

    @Autowired
    private HomeService homeService;
    @Autowired
    private WxMiniProgramUtil wxMiniProgramUtil;
    @Autowired
    private MemberService memberService;
    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/worksInfo")
    @ApiOperation(value = "作品详情" ,notes = "")
    public AppResponse worksInfo(@Valid @RequestBody WorksInfoRequest request ){
        return homeService.worksInfo(request);
    }

    @PostMapping("/followList")
    @ApiOperation(value = "关注的作品列表" ,notes = "")
    public AppResponse followList(@Valid @RequestBody RecommendRequest request ){
        return homeService.followList(request);
    }

    @PostMapping("/worksDetailsList")
    @ApiOperation(value = "作品详情列表" ,notes = "")
    public AppResponse worksDetailsList(@Valid @RequestBody WorksInfoRequest request ){
        return homeService.worksDetailsList(request);
    }


    @PostMapping("/recommend")
    @ApiOperation(value = "推荐列表" ,notes = "")
    public AppResponse recommend(@Valid @RequestBody RecommendRequest request ){
        return homeService.recommend(request);
    }

    @PostMapping("/follow")
    @ApiOperation(value = "关注作品" ,notes = "")
    public AppResponse follow(@Valid @RequestBody WorksInfoRequest request ){
        return homeService.follow(request);
    }

    @PostMapping("/cancelFollow")
    @ApiOperation(value = "取消关注" ,notes = "")
    public AppResponse cancelFollow(@Valid @RequestBody WorksInfoRequest request ){
        return homeService.cancelFollow(request);
    }

    @PostMapping("/give")
    @ApiOperation(value = "点赞" ,notes = "")
    public AppResponse give(@Valid @RequestBody WorksInfoRequest request ){
        return homeService.give(request);
    }

    @PostMapping("/visit")
    @ApiOperation(value = "增加浏览数" ,notes = "")
    public AppResponse addvisit(@Valid @RequestBody WorksInfoRequest request ){
        return homeService.addvisit(request);
    }

    @PostMapping("/reward")
    @ApiOperation(value = "打赏" ,notes = "")
    public AppResponse reward(@Valid @RequestBody RewardRequest request ){
        return homeService.reward(request);
    }

    @PostMapping("/rewardRecord")
    @ApiOperation(value = "打赏记录" ,notes = "")
    public AppResponse rewardRecord(@Valid @RequestBody RewardRecordRequest request ){
        return homeService.rewardRecord(request);
    }

    @PostMapping("/rewardConfigure")
    @ApiOperation(value = "打赏配置" ,notes = "")
    public AppResponse rewardConfigure(@Valid @RequestBody AppRequest request ){
        return homeService.rewardConfigure(request);
    }

    @PostMapping("/followUser")
    @ApiOperation(value = "关注用户" ,notes = "")
    public AppResponse followUser(@Valid @RequestBody WorksInfoRequest request ){
        return homeService.followUser(request);
    }


    @PostMapping("/preview")
    @ApiOperation(value = "预览(查看小程序码)" ,notes = "")
    public AppResponse preview(@Valid @RequestBody WorksRequest request , HttpServletResponse response){
        AppResponse response1 = new AppResponse();
        String accessToken = null;
        try{
            Member m =  memberService.findMemberByUid(request.getUserId());
            accessToken = WxQrCode.getAccessToken("wx59c864c7d2fc6536","3b6926886a620aed827c70a23b9a8fdd");
            WxParamModel wxParamModel = new WxParamModel();


            switch (request.getType()){
                case "0":
                    wxParamModel.setPage("pages/index/index");
                    wxParamModel.setScene(m.getPaintingNum());
                    break;
                case "1":
                    wxParamModel.setPage("pages/mine/paintList");
                    wxParamModel.setScene("t="+request.getAuthorId()+"&w="+request.getId()+"&u="+m.getPaintingNum()+"&f=share");
                    break;
            }

            String id = IdUtils.getNum14();
            redisUtil.set("share:"+id, JsonUtil.toJsonString(wxParamModel), 60*60*24);
            System.out.println("accessToken;"+accessToken);
            String twoCodeUrl = WxQrCode.getminiqrQr(accessToken,"pages/share/center",id);
            response1.setItem("https://qiniu.17huaduoduo.com/"+twoCodeUrl);
        }catch (Exception e){
            e.printStackTrace();
        }
        return response1;
    }

    @PostMapping("/getShareValue")
    @ApiOperation(value = "根据分享key获取值" ,notes = "")
    public AppResponse getShareValue(@Valid @RequestBody KeyRequest request ){
        AppResponse response = new AppResponse();
        response.setItem(redisUtil.get("share:"+request.getKey()));
        return response;
    }

    @PostMapping("/suggestedFollows")
    @ApiOperation(value = "推荐关注" ,notes = "")
    public AppResponse suggestedFollows(@Valid @RequestBody AppRequest request ){
        return homeService.suggestedFollows(request);
    }

    @PostMapping("/complaint")
    @ApiOperation(value = "投诉" ,notes = "")
    public AppResponse complaint(@Valid @RequestBody ComplaintRequest request ){
        return homeService.complaint(request);
    }

    @PostMapping("/feedback")
    @ApiOperation(value = "建议反馈" ,notes = "")
    public AppResponse feedback(@Valid @RequestBody FeedbackRequest request ){
        return homeService.feedback(request);
    }

    @PostMapping("/getPoster")
    @ApiOperation(value = "获取海报" ,notes = "")
    public AppResponse getPoster(@Valid @RequestBody WorksInfoRequest request ){
        return homeService.getPoster(request);
    }

    @PostMapping("/topicWorks")
    @ApiOperation(value = "话题作品列表" ,notes = "")
    public AppResponse topicWorks(@Valid @RequestBody TopicsRequest request ){
        return homeService.topicWorks(request);
    }

    @PostMapping("/topicWorksDetailsList")
    @ApiOperation(value = "话题作品详细列表" ,notes = "")
    public AppResponse topicWorksDetailsList(@Valid @RequestBody TopicsRequest request ){
        return homeService.topicWorksDetailsList(request);
    }


    @PostMapping("/isUpdate")
    @ApiOperation(value = "是否更新(黄点更新)" ,notes = "")
    public AppResponse isUpdate(@Valid @RequestBody AppRequest request ){
        return homeService.isUpdate(request);
    }


    @PostMapping("/resetUpdate")
    @ApiOperation(value = "重置更新" ,notes = "")
    public AppResponse resetUpdate(@Valid @RequestBody AppRequest request ){
        return homeService.resetUpdate(request);
    }



    @PostMapping("/share")
    @ApiOperation(value = "分享统计" ,notes = "")
    public AppResponse share(@Valid @RequestBody WorksInfoRequest request ){
        return homeService.share(request);
    }

    @PostMapping("/amountRest")
    @ApiOperation(value = "红包提示金额清理" ,notes = "")
    public AppResponse amountRest(@Valid @RequestBody WorksInfoRequest request ){
        return homeService.amountRest(request);
    }

    @PostMapping("/fansRest")
    @ApiOperation(value = "粉丝新增清理" ,notes = "")
    public AppResponse fansRest(@Valid @RequestBody WorksInfoRequest request ){
        return homeService.fansRest(request);
    }



}
